/*
function keyp() {
  if (event.keyCode == 38) {
    //document.getElementById("par").innerHTML ="按了上";
    document.getElementById("t1").style.backgroundColor = "#EE9A49";
    document.getElementById("t11").style.backgroundColor = "#D8BFD8";
  }
  if (event.keyCode == 37) {

    //document.getElementById("par").innerHTML ="按了左";
    document.getElementById("t2").style.backgroundColor = "#EEA2AD";
    document.getElementById("t8").style.backgroundColor = "#5CACEE";
  }
  if (event.keyCode == 39) {
    //document.getElementById("par").innerHTML ="按了右";
    document.getElementById("t3").style.backgroundColor = "#D8BFD8";
    document.getElementById("t9").style.backgroundColor = "#EE9A49";
  }
  if (event.keyCode == 40) {

    //document.getElementById("par").innerHTML ="按了下";
    document.getElementById("t4").style.backgroundColor = "#5CACEE";
    document.getElementById("t10").style.backgroundColor = "#EEA2AD";

  }
}

document.onkeydown = keyp;
*/

/*
//坐标

function pos(n) {
  var m;
  if (n <= 50) {
    return m = 0;
  }
  if (n >= 50 && n <= 100) {
    return m = 1;
  }
  if (n >= 100 && n <= 150) {
    return m = 2;
  }
  if (n >= 150 && n <= 200) {
    return m = 3;
  }
  if (n >= 200 && n <= 250) {
    return m = 4;
  }
  if (n >= 250 && n <= 300) {
    return m = 5;
  }
}
//点击操作
var r = 3, l = 2;
function tdp() {
  var tdi = "b";
  document.getElementById(tdi + r + l).style.backgroundColor = "";
  if (event.touches.length == 1) {
    var xT = event.touches[0].pageX;
    var yT = event.touches[0].pageY;
  }
  if (xT > 300 || yT > 300) {
    alert("这是外面!");
    document.getElementById(tdi + r + l).style.backgroundColor = "#EEA2AD";
    return;
  }
  l = pos(xT);
  r = pos(yT);
  document.getElementById(tdi + r + l).style.backgroundColor = "#5CACEE";
  if (r == 0 || r == 2 || r == 4) {
    document.getElementById(tdi + r + l).style.backgroundColor = "#EE9A49";
  }
  document.getElementById("bid").innerHTML = "点击的坐标：" + xT + ";" + yT;
}
function phtouch() {
  /*
   var x = document.getElementById("b01").offsetLeft
   var y = document.getElementById("b01").offsetTop
   var wi = document.getElementById('b01').offsetWidth
   var he = document.getElementById('b01').offsetHeight
   */
  
  /*
  tdp();
}
//手移开操作
function phedtouch() {

  //document.getElementById("bid").innerHTML = "";

}

document.addEventListener("touchstart", phtouch, false);
document.addEventListener("touchend", phedtouch, false);

*/
//画表格
function tab() {
  var tx ="";
		for (var x = 0; x < 6; x++) {
    tx += "<tr>";
    for (var y = 0; y < 6; y++) {
      tx += "<td id=b" + x + y + "></td>";
    }
    tx += "</tr>";
		}
  document.getElementById("ta").innerHTML = tx;
  document.getElementById("b20").style.backgroundColor = "#EEA2AD";
  lmv();
  keyp();

}
//开始时自动移动
function lmv() {
  var lm;
  moved(i, j);
    lm = setTimeout("lmv()", 600);
  if (dir == 37 || dir == 38 || dir == 39 || dir == 40 || dir == 32) {
    j=j-1;
    clearTimeout(lm);
  }
  j++;
  j = edge(j);
  move(i, j);
 
}

window.onload = tab;
var i = 2, j = 0;
var dir, dired;
var aut;
//键盘方向按键
function keyp() {
  moved(i, j);
  switch (dir) {
    case 38:
      i--;
      break;
    case 40:
      i++;
      break;
    case 37:
      j--;
      break;
    case 39:
      j++;
      break;
    case 32:
        pau();
  }
  i = edge(i);
  j = edge(j);
  move(i, j);
  aut = setTimeout("keyp()", 500);
}
//边界
function edge(c) {
  if (c < 0) {
    alert("边界");
    return c = 0;
  } else
    if (c > 5) {
      alert("边界");
      return c = 5;
    } else {
      return c;
    }
}
//暂停
function pau() {
  if (dir == 32) {
    clearTimeout(aut);
  }
}

//键盘方向键后动作
function move(a, b) {
  document.getElementById("b" + a + b).style.backgroundColor = "#EEA2AD";
}
function moved(a, b) {
  document.getElementById("b" + a + b).style.backgroundColor = "";
}

function dr() {
  dired = dir;
  dir = event.keyCode;
  if (dired == 37 && dir == 39) {
    dir = 37;
  }
  if (dired == 38 && dir == 40) {
    dir = 38;
  }
  if (dired == 39 && dir == 37) {
    dir = 39;
  }
  if (dired == 40 && dir == 38) {
    dir = 40;
  }
  if (dir == 32) {
    dir = 32;
  }
}
document.onkeydown = dr;